// packageFT/hotel/matching-room/matching-room.js
const app = getApp()
import { roomTypeEnum } from '../../../utils/dataEnum'
import { dateRange } from '../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nid: 0,
    demandData: {},
    roomList: [],
    checkedRoomID: 0,
    checkedRoom: {},
    roomTypeEnum
  },

  /**
   * 获取匹配房间信息
   */
  matchingRoom: function() {
    const { nid } = this.data
    if (!nid) return
    const urlStr = app.getNetAddresss('plugin.hotel.frontend.hotel.hotelDemand.matching-rooms')
    const _this = this
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url: urlStr,
      data: {id: nid},
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          let { list, hotel_demand } = resData.data
          if (hotel_demand.start_check_in_time && hotel_demand.end_check_in_time) {
            hotel_demand.timeRange = dateRange(hotel_demand.start_check_in_time, hotel_demand.end_check_in_time, true)
          }
          _this.setData({ 
            roomList: list.data,
            demandData: hotel_demand
          })
        } else {
          wx.showModal({
            title: '提示',
            content: resData.msg || '匹配失败！',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                // wx.navigateBack({ delta: -1 })
              }
            }
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 房间选中
   * @param {Object}} e 
   */
  roomSelect: function(e) {
    const { detail } = e
    const { roomList } = this.data
    const room = roomList.find(item => item.id == detail)
    this.setData({ 
      checkedRoomID: detail,
      checkedRoom: room
    })
  },

  /**
   * 提交
   */
  submitMatching: function() {
    const { demandData, checkedRoom, nid } = this.data
    if (!Object.keys(demandData).length) {
      app.tips('无酒店需求信息！')
      return
    } 
    if (!Object.keys(checkedRoom).length) {
      app.tips('请选择要匹配的房间！')
      return
    }
    const hotel_id = checkedRoom?.has_one_hotel_goods?.has_one_hotel?.id
    if (!hotel_id) {
      app.tips('无法获取酒店id')
      return
    }
    const now = new Date()
    const { valid_time } = demandData
    const valid = new Date(valid_time.replace(/-/g, '/'))
    const timeDiff = valid - now
    if (timeDiff <= 0) {
      app.tips('需求订单已超时！')
      return
    }
    const data = {
      hotel_id,
      is_buy_it_now: 0,
      hotel_demand_id: nid,
      enter_at: demandData.start_check_in_time,
      leave_at: demandData.end_check_in_time,
      dispatch_type_id: 4,
      member_coupon_ids: [],
      goods: JSON.stringify([{
        goods_id: checkedRoom?.id,
        goods_option_id: '',
        total: demandData.room_num,
      }]),
      address: JSON.stringify({}),
      orders: JSON.stringify([]),
      mobile: demandData.mobile,
      realname: demandData.username,
      invoice_type: 0,
      rise_type: 1,
    }
    const urlStr = app.getNetAddresss('plugin.hotel.frontend.hotel.create')
    wx.showLoading({
      title: '加载中...',
    })
    app._getNetWork({
      url: urlStr,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          wx.showModal({
            title: '成功',
            content: '房间匹配成功！',
            showCancel: false,
            complete: (resC) => {
              if (resC.confirm) {
                const { tmplIds } = app.data
                wx.requestSubscribeMessage({
                  tmplIds: tmplIds[0],
                  complete: function() {
                    const pages = getCurrentPages()
                    //获取上一个页面的实例
                    const beforePage = pages[pages.length - 3]
                    //1、调用上一个页面onLoad
                    beforePage.onLoad() 
                    //2、调用上一个页面刷新方法
                    beforePage.refreshPage()
                    wx.navigateBack({ delta: 2 })
                  }
                })
              }
            }
          })
        } else {
          app.tips(resData.msg || '匹配失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ nid: options.nid || 0})
    this.matchingRoom()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})